package com.night.attendance.consumer;

import cn.hutool.core.date.DateUtil;
import com.night.attendance.service.StudentAttendanceService;
import com.night.common.entity.attendance.AttendanceMessageDO;
import com.night.common.entity.attendance.StudentAttendanceDO;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Dark-Morning
 * 2022/10/20 15:12
 */
@Component
@SuppressWarnings("all")
public class AttendanceConsumer {
    @Autowired
    StudentAttendanceService studentAttendanceService;

    @RabbitListener(queues = "attendance-death-queue")
    public void getMessage(AttendanceMessageDO attendanceMessageDO, Message message, Channel channel) throws IOException {
        StudentAttendanceDO studentAttendanceDO = new StudentAttendanceDO();
        studentAttendanceDO.setStudentId(attendanceMessageDO.getStudentId());
        studentAttendanceDO.setAttendanceConfigId(attendanceMessageDO.getAttendanceConfigId());
        if (studentAttendanceService.getById(studentAttendanceDO) == 0) {
            studentAttendanceDO.setSignTime(DateUtil.now());
            studentAttendanceDO.setSignState("2");
            studentAttendanceService.updateStudentAttendance(studentAttendanceDO);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
